import connDB



def getCreateTableSql(schema,conn):
    if conn:
        with open("read/批量建表.csv", "r", encoding='utf8') as csv:
            with open("out/建表语句.sql", "w", encoding='utf8') as out:
                for line in csv:
                    tableName = line.strip()
                    query = f"select pg_get_tabledef('{schema}.{tableName}');"
                    createTableSql = connDB.fetch_data(conn, query)
                    if createTableSql:
                        out.write(createTableSql[0][0]+'\n\n\n')
                        print(f"已写入建表语句：{tableName}")
                    else:
                        print(f"未找到建表语句：{tableName}")
if __name__ == "__main__":
    schema = 'dt_yunnan'
    #数据库连接
    conn = connDB.create_connection(**connDB.dm_prd_config)
    getCreateTableSql(schema,conn)